package ch.zbw.sysventory.server.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ch.zbw.sysventory.server.model.Computer;
import ch.zbw.sysventory.server.model.ComputerScan;
import ch.zbw.sysventory.server.model.ComputerScanResult;
import ch.zbw.sysventory.server.repository.ComputerScanResultRepository;
import ch.zbw.sysventory.server.repository.ComputerScanResultRepositoryCustom;
import ch.zbw.sysventory.server.service.ComputerScanResultService;
import ch.zbw.sysventory.server.service.ComputerScanService;

@Transactional
@Service
public class ComputerScanResultServiceImpl implements ComputerScanResultService {

	@Autowired(required = true)
	private ComputerScanResultRepository computerScanResultRepository;

	@Autowired(required = true)
	private ComputerScanResultRepositoryCustom computerScanResultRepositoryCustom;

	@Autowired(required = true)
	private ComputerScanService computerScanService;

	@Override
	public ComputerScanResult get(Long id) {
		return this.computerScanResultRepository.findOne(id);
	}

	@Override
	public List<ComputerScanResult> listByComputerScan(ComputerScan computerScan) {
		return this.computerScanResultRepository.findByComputerScan(computerScan);
	}

	@Override
	public List<ComputerScanResult> listByComputer(Computer computer) {
		return this.computerScanResultRepository.findByComputer(computer);
	}

	@Override
	public ComputerScanResult save(ComputerScanResult computerScanResult) {
		return this.computerScanResultRepository.saveAndFlush(computerScanResult);
	}

	@Override
	public List<ComputerScanResult> searchComputerScans(String searchCombination, String computerName, String ipAddress, String type, String cpuType, String operatingSystem) {
		return this.computerScanResultRepositoryCustom.getComputerScanSearchResults(computerScanService.getLatestScan(), searchCombination, computerName, ipAddress, type, cpuType,
				operatingSystem);
	}

	@Override
	public List<ComputerScanResult> listLatestScanResultsByComputers(List<Computer> computers) {
		List<ComputerScanResult> scanResults = new ArrayList<ComputerScanResult>();
		Iterator<Computer> computerIterator = computers.iterator();
		while (computerIterator.hasNext()) {
			Computer computer = computerIterator.next();
			List<ComputerScanResult> computerScanResults = this.computerScanResultRepository.findByComputerOrderByTimeStampDesc(computer);
			if (computerScanResults.size() > 0)
				scanResults.add(computerScanResults.get(0));
		}
		return scanResults;
	}

	@Override
	public List<String> listAllOperatingSystemsForLatestScan(ComputerScan latestScan) {
		List<ComputerScanResult> scanResults = this.listByComputerScan(latestScan);
		List<String> operatingSystems = new ArrayList<String>();
		Iterator<ComputerScanResult> scanResultsIterator = scanResults.iterator();
		while (scanResultsIterator.hasNext()) {
			ComputerScanResult computerScanResult = scanResultsIterator.next();
			if (computerScanResult.getOperatingSystem() != null && !"".equals(computerScanResult.getOperatingSystem())) {
				if (!operatingSystems.contains(new String(computerScanResult.getOperatingSystem())))
					operatingSystems.add(computerScanResult.getOperatingSystem());
			}
		}
		Collections.sort(operatingSystems);
		return operatingSystems;
	}
}
